#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    if(argc < 2) {
        printf("errore\n\r");
        return 0;
    }

    if(!strcmp(argv[1], "home")) {
        if(argc < 3) {
            printf("errore\n\r");
            return 0;
        }

        char home[256];
        char cmd[256];
        
        snprintf(home, sizeof(home), "/home/projects/%s", argv[2]);

        snprintf(cmd, sizeof(cmd), "/bin/mkdir %s", home);
        
        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }

        snprintf(cmd, sizeof(cmd), "/bin/chown %s:%s %s", argv[3], argv[3], home);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "/bin/chmod 2770 %s", home);
        
        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
                                         
        return 0;
    } else if(!strcmp(argv[1], "svn")) {
        if(argc < 3) {
            printf("errore\n\r");
            return 0;
        }
        char svn[256];
        char cmd[256];
        
        snprintf(svn, sizeof(svn), "/svnroot/%s", argv[2]);
        
        snprintf(cmd, sizeof(cmd), "/usr/bin/svnadmin create %s", svn);        

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "/bin/chgrp -R %s %s", argv[3], svn);
        
        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "find %s -type f -exec chmod 660 {} \\;", svn);        
        
        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "find %s -type d -exec chmod 2770 {} \\;", svn);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
    } else if(!strcmp(argv[1], "newlist")) {
        if(argc < 3) {
            printf("errore\n\r");
            return 0;
        }
        char cmd[256];

        snprintf(cmd, sizeof(cmd), "/usr/sbin/newlist -q %s admin@softwarelowcost.it %s", argv[2], argv[3]);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
    } else if(!strcmp(argv[1], "dellist")) {
        if(argc < 2) {
            printf("errore\n\r");
            return 0;
        }
        char cmd[256];

        snprintf(cmd, sizeof(cmd), "/usr/sbin/rmlist %s", argv[2]);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }

    }
                                                             
    
    return 0;
}
